package com.hmall.service;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hdmall.business.common.common.ServerResponse;
import com.hmall.mapper.SysQuestionAchievementMapper;
import com.hmall.mapper.SysQuestionAnswerMapper;
import com.hmall.mapper.SysQuestionMapper;
import com.hmall.model.SysQuestion;
import com.hmall.model.SysQuestionAchievement;
import com.hmall.vo.QuestionItemVo;
import com.hmall.vo.QuestionJson;
import com.hmall.vo.QuestionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @ClassName QuestionService
 * @Description
 * @Author zhoutao
 * @Date 2019/1/28 8:57
 * @Version 2.0
 **/
@Service
public class QuestionService {

    private final
    SysQuestionMapper questionMapper;

    private final
    SysQuestionAchievementMapper questionAchievementMapper;

    private final
    SysQuestionAnswerMapper questionAnswerMapper;

    @Autowired
    public QuestionService(SysQuestionAchievementMapper questionAchievementMapper, SysQuestionMapper questionMapper, SysQuestionAnswerMapper questionAnswerMapper) {
        this.questionAchievementMapper = questionAchievementMapper;
        this.questionMapper = questionMapper;
        this.questionAnswerMapper = questionAnswerMapper;
    }

    /**
     * @return com.hmall.common.ServerResponse
     * @Date 2019/1/28 13:30
     * @Param []
     * @Description 返回问题
     */
    public ServerResponse getQuestion() {

        //5道多选5道单选
        List<QuestionVo> questionVoList = new ArrayList<>();

        //单选题类型
        int simpleType = 1;
        //多选题类型
        int complexType = 2;
        //单选题数目
        int simpleCount = 5;
        //多选题数目
        int complexCount = 5;

        questionVoList.addAll(addQuesitonByType(simpleType, simpleCount));
        questionVoList.addAll(addQuesitonByType(complexType, complexCount));
        return ServerResponse.createBySuccess(questionVoList);
    }

    /**
     * @Date 2019/1/28 14:05
     * @Param [type, initCount] 
     * @return java.util.List<com.hmall.vo.QuestionVo>
     * @Description 根据类型和初始数量返回出的题目
     */
    private List<QuestionVo> addQuesitonByType(int type, int initCount) {

        List<QuestionVo> questionVoList = new ArrayList<>();
        List<Integer> questionList = questionMapper.selectIdByType(type);
        int initSize = questionList == null ? 0 : questionList.size();

        int count = Math.min(initSize, initCount);
        Set<Integer> questionIdSet = new HashSet<>();


        while (questionIdSet.size() < count) {
            int index = (int) (Math.random() * initSize);
            if (questionIdSet.contains(index)) {
                continue;
            }
            questionIdSet.add(index);
        }


        for (Object aSimpleSet : questionIdSet) {
            assert questionList != null;
            int index = questionList.get((Integer) aSimpleSet);
            SysQuestion sysQuestion = questionMapper.selectByPrimaryKey(index);
            List<QuestionItemVo> questionItemVoList = questionAnswerMapper.selectByQuestionId(sysQuestion.getId());
            questionVoList.add(new QuestionVo(sysQuestion.getId(), sysQuestion.getQuestionName(), questionItemVoList, type));
        }
        
        return questionVoList;
    }

    /**
     * @Date 2019/1/28 14:10
     * @Param [userId, json] 
     * @return com.hmall.common.ServerResponse
     * @Description 得到答案
     */
    public ServerResponse postQuestion(int userId, String json) {

        //如果已经答过题目了，直接返回答案，否则要计算一下插入表
        SysQuestionAchievement questionAchievement = questionAchievementMapper.selectByUserId(userId);
        if(questionAchievement != null){
            return ServerResponse.createBySuccess(questionAchievement.getAchievement());
        }
        Gson gson = new Gson();
        List<QuestionJson> questionVoList = gson.fromJson(json, new TypeToken<List<QuestionJson>>() {}.getType());

        int achievement = 0;
        for(QuestionJson questionJson : questionVoList){

            //去匹配答案
            String answer = questionMapper.selectByPrimaryKey(questionJson.getQuestionId()).getAnswerId();
            if(answer.equals(questionJson.getQuestionAnswer())){
                achievement += 10;
            }
        }
        int maxRemark = 100;
        Map map = new HashMap<>();
        map.put("remark", Math.min(maxRemark, achievement));
        return ServerResponse.createBySuccess(map);
    }
}
